const router = require('koa-router')();
router.prefix(appConfig.authPrefixUrl+'/desktop/sys_user');
const SysUserService = appRequire('service/desktop/SysUserService.js');
const SysRoleService = appRequire('service/desktop/SysRoleService.js');
const ResultVo = appRequire('vo/ResultVo.js');

router.get('/index',async(ctx)=>{
  ctx.body="sys_user 你好";
});
// router.post('/login',async(ctx)=>{
//   console.log(ctx.request.body);
//   let user = ctx.request.body;
//   console.log(appConfig.db[user.username]);
//   if(appConfig.db[user.username]){
//     if(appConfig.db[user.username].username===user.username&&appConfig.db[user.username].password===user.password){
//       ctx.session.user = appConfig.db[user.username];
//       // 授权首页和欢迎页
//       // 授权create menu
//       ctx.session.user.authUrls.push('/dicms/static/desktop/ui1/index.html');
//       ctx.session.user.authUrls.push('/dicms/api/desktop/index/init');
//       ctx.session.user.authUrls.push('/dicms/static/desktop/ui1/page/welcome.html');

//       ctx.session.user.authUrls.push('/dicms/static/desktop/ui1/page/user*');
//       ctx.session.user.authUrls.push('/dicms/static/desktop/ui1/page/role*');
//       ctx.session.user.authUrls.push('/dicms/static/desktop/ui1/page/auth*');
//       ctx.session.user.authUrls.push('/dicms/static/desktop/ui1/page/type*');
//       ctx.session.user.authUrls.push('/dicms/static/desktop/ui1/page/icon*');

//       ctx.session.user.authUrls.push('/dicms/api/desktop/sys_user*');
//       ctx.session.user.authUrls.push('/dicms/api/desktop/sys_role*');
      // ctx.session.user.authUrls.push('/dicms/api/desktop/sys_auth*');
//       ctx.session.user.authUrls.push('/dicms/api/desktop/sys_auth_type*');
//       ctx.session.user.authUrls.push('/dicms/api/desktop/sys_icon*');

//       ctx.body = ResultVo.success('success',ctx.session.user);
//     }
//   }else{
//     ctx.body=ResultVo.error(1,'error','登录失败!');
//   }
// });
router.post('/login',async(ctx)=>{
  try{
    console.log(ctx.request.body);
    let user = ctx.request.body;
    let dbUser = await SysUserService.findOneByUsernameAndPassword(user);
    console.log("dbUser:"+JSON.stringify(dbUser));
    if(dbUser){
      if(dbUser.username===user.username&&dbUser.password===user.password){
        let data = await SysUserService.findUserAuthItem(dbUser.id);
        // user.authUrls = data.authUrls;
        user.authUrls = initAllowedUrls(data.authUrls);
        user.menuInfo = data.authMenus;
        user.id = dbUser.id;
        user.password = "";
        appConfig.db[user.username] = user;
        ctx.session.user = {id:dbUser.id,username:dbUser.username};
        ctx.body = ResultVo.success('success',user);
      }else{
        ctx.body=ResultVo.error(1,'error','登录失败!');
      }
    }
    else{
      ctx.body=ResultVo.error(1,'error','登录失败!');
    }
  }catch(err){
    //集中处理错误
    err.msg = "数据信息后台登录验证失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.post('/logout',async(ctx)=>{
  console.log(ctx.request.body);
  let user = ctx.request.body;
  // 删除服务端session
  ctx.session = null;
  ctx.body = ResultVo.success('success','退出成功!');
});

router.post('/save',async(ctx)=>{
  try{
    let user = ctx.request.body;
    console.log(user);
    await SysUserService.save(user);
    ctx.body = ResultVo.success('success');
  }catch(err){
    //集中处理错误
    err.msg = "新增用户失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/deleteOne',async(ctx)=>{
  try{
    let user = ctx.query;
    user = await SysUserService.deleteOne(user.id);
    ctx.body = ResultVo.success('success',user);
  }catch(err){
    //集中处理错误
    err.msg = "删除用户失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.post('/deleteMany',async(ctx)=>{
  try{
    let param = ctx.request.body;
    console.log(param);
    param = await SysUserService.deleteMany(param.ids);
    ctx.body = ResultVo.success('success',param);
  }catch(err){
    //集中处理错误
    err.msg = "批量删除用户失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findOne',async(ctx)=>{
  try{
    let user = ctx.query;
    user = await SysUserService.findOne(user.id);
    let roles = await SysRoleService.findAll();
    user.roles = roles;
    console.log(user);
    ctx.body = ResultVo.success('success',user);
  }catch(err){
    //集中处理错误
    err.msg = "查找用户失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findAll',async(ctx)=>{
  try{
    let data = await SysUserService.findAll();
    ctx.body = ResultVo.success('success',data);
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取全部用户数据失败!');
  }
});
router.get('/findByPage',async(ctx)=>{
  try{
    let page = ctx.query;
    console.log(page);
    let data = await SysUserService.findByPage(page);
    ctx.body = data;
  }catch(error){
    console.log(error);
    ctx.body = ResultVo.error(-1,'获取分页用户数据失败!');
  }
});


module.exports = router;